package com.pet.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.pet.service.SysRoleService;
import com.pet.system.SysRole;
import com.pet.utils.Result;
import com.pet.vo.AssginRoleVo;
import com.pet.vo.SysRoleQueryVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * <p>
 * 角色 前端控制器
 * </p>
 *
 * @author whd
 * @since 2023-07-29
 */
@Api(tags = "角色表前端控制器")
@RestController

@RequestMapping("/lovePet/system/sysRole")
@CrossOrigin("*")
public class SysRoleController {

    @Autowired
    private SysRoleService sysRoleService;

    @ApiOperation("获取全部角色列表")
    @GetMapping("/findAll")
    public Result findAll() {
        List<SysRole> list = this.sysRoleService.list();
        return Result.ok(list);
    }

    // 测试删除
    @PreAuthorize("hasAuthority('bnt.sysRole.remove')")
    @ApiOperation("根据ID删除角色")
    @DeleteMapping("/removeRole/{id}")
    public Result removeRole(@ApiParam("角色ID") @PathVariable Long id) {
        boolean b = this.sysRoleService.removeById(id);
        return b ? Result.ok() : Result.fail();
    }


    // 分页和条件查询
    @ApiOperation("分页和条件查询角色")
    @PreAuthorize("hasAuthority('bnt.sysRole.list')")
    @GetMapping("/{page}/{limit}")
    public Result findRoleByPageQuery(@ApiParam("页码") @PathVariable Long page,
                                      @ApiParam("每页条数") @PathVariable Long limit,
                                      @ApiParam("包装的查询角色名称") SysRoleQueryVo sysRoleQueryVo) {
        //1.创建分页对象
        IPage<SysRole> p1 = new Page<SysRole>(page, limit);
        //2.调用方法
        p1 = this.sysRoleService.selectpage(p1, sysRoleQueryVo);
        //3.返回
        return Result.ok(p1);
    }

    @ApiOperation("添加角色")
    @PreAuthorize("hasAuthority('bnt.sysRole.add')")
    @PostMapping("/addRole")
    public Result addRole(@ApiParam("角色信息") @RequestBody SysRole sysRole) {
        boolean res = this.sysRoleService.save(sysRole);
        return res ? Result.ok() : Result.fail();
    }

    // 修改
    //1.根据id 去得到一个角色
    @PreAuthorize("hasAuthority('bnt.sysRole.list')")
    @ApiOperation("根据ID获取角色")
    @GetMapping("/findRoleById/{id}")
    public Result findRoleById(@ApiParam("角色ID") @PathVariable Long id) {
        SysRole sysRole = this.sysRoleService.getById(id);
        return Result.ok(sysRole);
    }


    @ApiOperation("更新角色")
    @PreAuthorize("hasAuthority('bnt.sysRole.update')")
    @PostMapping("/updateRole")
    public Result updateRole(@ApiParam("角色信息") @RequestBody SysRole sysRole) {
        boolean b = this.sysRoleService.updateById(sysRole);
        return b ? Result.ok() : Result.fail();
    }


    @PreAuthorize("hasAuthority('bnt.sysRole.remove')")
    @ApiOperation("批量删除角色")
    @DeleteMapping("/removeRoleByIds")
    public Result removeRoleByIds(@ApiParam("待删除的角色ID列表") @RequestBody List<Long> ids) {
        boolean b = this.sysRoleService.removeBatchByIds(ids);
        return b ? Result.ok() : Result.fail();
    }


    // 根据用户获取角色信息
    @ApiOperation("根据用户获取角色信息")
    @GetMapping("/toAssign/{userId}")
    public Result toAssign(@ApiParam("用户ID") @PathVariable Long userId) {
        Map<String, Object> map = this.sysRoleService.getRolesByUserId(userId);
        return Result.ok(map);
    }

    // 根据用户分配角色
    @ApiOperation("根据用户分配角色")
    @PostMapping("/doAssign")
    public Result doAssign(@ApiParam("分配角色信息") @RequestBody AssginRoleVo assginRoleVo) {
        this.sysRoleService.doAssign(assginRoleVo);
        return Result.ok();
    }
}

